package com.china08.yxyapi.api.home.index.news;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.platform.YxyHomeNews;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeNewsRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.DisposeCkContentUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 首页--新闻详情查询
 * 
 */
@CrossOrigin
@RestController
public class IndexSrhNewsDetailController extends YxyApiBaseController {

    @Autowired
    private YxyHomeNewsRepository yxyHomeNewsRepository;

    @ApiOperation(value = "首页咨询详情")
    @ResponseBody
    @RequestMapping(value = "/api_yw/home/index/news/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody(required = false) RepModel reqModel) {

        String schoolId = reqModel.schoolId;

        int status = 1;
        YxyHomeNews yxyHomeNews = yxyHomeNewsRepository.findByIdAndEdbIdAndStatus(reqModel.id, schoolId, status);
        Assert.isTrue(yxyHomeNews != null, "不存在该内容");

        // 增加浏览量
        yxyHomeNews.setPv(yxyHomeNews.getPv() == null ? 0 : yxyHomeNews.getPv() + 1);
        yxyHomeNewsRepository.save(yxyHomeNews);

        String con = yxyHomeNews.getContent();
        con = DisposeCkContentUtil.disposeHeight(con);
        yxyHomeNews.setContent(con);
        RespModel respModel = new RespModel();
        respModel.id = yxyHomeNews.getId();
        respModel.edbId = yxyHomeNews.getEdbId();
        respModel.status = yxyHomeNews.getStatus();
        respModel.title = yxyHomeNews.getTitle();
        respModel.content = yxyHomeNews.getContent();
        respModel.createdBy = yxyHomeNews.getCreatedBy();
        respModel.createdDate = DateToolUtils.format(yxyHomeNews.getCreatedDate(), DateToolUtils.FORMAT_SHORT_YMD);
        Date currentDate = yxyHomeNews.getCreatedDate();
        YxyHomeNews prevYxyHomeNews = yxyHomeNewsRepository
                .findTop1ByEdbIdAndStatusAndCreatedDateGreaterThanOrderByCreatedDateAsc(schoolId, status, currentDate);
        YxyHomeNews nextYxyHomeNews = yxyHomeNewsRepository
                .findTop1ByEdbIdAndStatusAndCreatedDateLessThanOrderByCreatedDateDesc(schoolId, status, currentDate);
        YxyHomeNewsIdAndTitle prevYxyHomeNewsIdAndTitle = null;
        YxyHomeNewsIdAndTitle nextYxyHomeNewsIdAndTitle = null;
        if (prevYxyHomeNews != null) {
            prevYxyHomeNewsIdAndTitle = new YxyHomeNewsIdAndTitle();
            prevYxyHomeNewsIdAndTitle.id = prevYxyHomeNews.getId();
            prevYxyHomeNewsIdAndTitle.title = prevYxyHomeNews.getTitle();
        }
        if (nextYxyHomeNews != null) {
            nextYxyHomeNewsIdAndTitle = new YxyHomeNewsIdAndTitle();
            nextYxyHomeNewsIdAndTitle.id = nextYxyHomeNews.getId();
            nextYxyHomeNewsIdAndTitle.title = nextYxyHomeNews.getTitle();
        }

        respModel.prev = prevYxyHomeNewsIdAndTitle;
        respModel.next = nextYxyHomeNewsIdAndTitle;
        return respModel;
    }

    public class YxyHomeNewsIdAndTitle {
        public String id;
        public String title;
    }

    protected static class RepModel extends YxyApiReqModel {
        // 新闻id
        public String id;
        // 域名
        public String domain;
        // 学校id
        public String schoolId;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "请指定咨询");
            Assert.notNull(StringUtils.defaultIfBlank(domain, null), "域名发生错误，请刷新页面");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        // 新闻id
        public String id;
        // 教育局id
        public String edbId;
        // 发布状态
        public int status;
        // 标题
        public String title;
        // 内容
        public String content;
        // 创建时间
        public String createdDate;
        public String createdBy;
        // 上一条新闻
        public YxyHomeNewsIdAndTitle prev;
        // 下一条新闻
        public YxyHomeNewsIdAndTitle next;
    }
}
